<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Reimburse;
use App\Models\ReimburseDetail;
use Illuminate\Support\Facades\Log;

class ReimburseService
{
    public function get($type){
        $sql = Reimburse::with('application')->with('project')->with('user');
        if($type == 'material'){
            $sql->with('materialContract');
        }
        if($type == 'labor'){
            $sql->with('laborContract');
        }
        return $sql;
    }

    public function insert($input,$details){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        $arr = [];
        foreach ($details as $key => $value) {
            $arr_ = [];
            $arr_['title'] = $value->title;
            $arr_['type'] = $value->type;
            $arr_['project'] = $value->project;
            $arr_['project_id'] = $input['project_id'];
            $arr_['status'] = $value->status;
            $arr_['remarks'] = $value->remarks;
            $arr_['price'] = $value->price;
            $arr_['nums'] = $value->nums;
            $arr_['unit'] = $value->unit;
            $arr_['tax_rate'] = $value->tax_rate;
            $arr_['tax_price'] = $value->tax_price;
            $arr_['audit_price'] = $value->audit_price;
            $arr_['invoice'] = $value->invoice;
            $arr_['reimburse_id'] = 0;
            $arr_['created_at'] = Date("Y-m-d H:i:s",time());
            $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
            $arr[] = $arr_;
        }

        try {
            $data = Reimburse::create($res);
            foreach ($arr as $key => $value) {
                $arr[$key]['reimburse_id'] = $data->id;
            }
            ReimburseDetail::insert($arr);
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input,$details){
        $res = [];
        foreach ($input as $k => $v) {
            $v = $v === null ? '/' : $v;
            if($v){
                $res[$k] = $v;
            }
        }
        $arr = [];
        foreach ($details as $key => $value) {
            $arr_ = [];
            $arr_['title'] = $value->title;
            $arr_['type'] = $value->type;
            $arr_['project'] = $value->project;
            $arr_['project_id'] = $input['project_id'];
            $arr_['status'] = $value->status;
            $arr_['remarks'] = $value->remarks;
            $arr_['price'] = $value->price;
            $arr_['nums'] = $value->nums;
            $arr_['unit'] = $value->unit;
            $arr_['tax_rate'] = $value->tax_rate;
            $arr_['tax_price'] = $value->tax_price;
            $arr_['audit_price'] = $value->audit_price;
            $arr_['invoice'] = $value->invoice;
            $arr_['reimburse_id'] = $res['id'];
            $arr_['created_at'] = Date("Y-m-d H:i:s",time());
            $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
            $arr[] = $arr_;
        }
        unset($res['id']);
        try {
            $data = Reimburse::where('id',$input['id'])->update($res);
            ReimburseDetail::where('reimburse_id',$input['id'])->delete();
            ReimburseDetail::insert($arr);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException($th);
        }
    }

}